package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.cookbook.dao.RecipeTypeDAO;
import com.wanmait.cookbook.dao.RecipeTypeSecondDAO;
import com.wanmait.cookbook.vo.RecipeTypeSecond;

/**
 * Servlet implementation class RecipeTypeServlet
 */
@WebServlet("/RecipeTypeServlet")
public class RecipeTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RecipeTypeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action");
		if ("getRecipeTypeSecondByRecipeTypeId".equals(action)) {
			this.getRecipeTypeSecondByRecipeTypeId(request, response);
		}else if ("findRecipeTypeSecondsBySecondId".equals(action)) {
		      this.findRecipeTypeSecondsBySecondId(request, response);
	    }
	}
	
	 // 通过二级id 查找同类的二级食谱  草稿箱 用
	  protected void findRecipeTypeSecondsBySecondId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    String recipeTypeId = request.getParameter("recipeTypeId");
	    List<RecipeTypeSecond> secondsRecipeType = RecipeTypeSecondDAO.getRecipeTypeSecondDAO().findRecipeTypeSecondsBySecondId(Integer.parseInt(recipeTypeId));
//	    response.getWriter().print(secondsRecipeType);
	    request.setAttribute("allRecipeTypeSeconds", secondsRecipeType);
	  
	    request.getRequestDispatcher("/inc/ajaxInc/showRecipeTypeSeconds.jsp").forward(request, response);
	  }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	
	// 涓嬫媺鍒楄〃妗嗚幏寰椾簩绾у垎绫�
	protected void getRecipeTypeSecondByRecipeTypeId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String recipeTypeId = request.getParameter("recipeTypeId");
		List<RecipeTypeSecond> allRecipeTypeSeconds = RecipeTypeDAO.getRecipeTypeDAO().findRecipeTypeSecondsByRecipeTypeId(Integer.parseInt(recipeTypeId));
		request.setAttribute("allRecipeTypeSeconds", allRecipeTypeSeconds);
		request.getRequestDispatcher("/inc/ajaxInc/showRecipeTypeSeconds.jsp").forward(request, response);
	}

}
